package makabakaquestionnaire.zutsoftware.mapper.typeHandler;

import makabakaquestionnaire.zutsoftware.utils.JsonUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * 问题子项expand的转换器
 * jdbc：json
 * JavaType：map
 */
//@MappedTypes({Object.class})
//@MappedJdbcTypes(JdbcType.OTHER)
public class expandHandler implements TypeHandler<Object> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        if (parameter==null){
            ps.setString(i,null);
            return;
        }
        String json= JsonUtil.toJson(parameter);
        ps.setString(i,json);
    }

    @Override
    public Object getResult(ResultSet rs, String columnName) throws SQLException {
        String json=rs.getString(columnName);
        return  JsonUtil.toMap(json);
    }

    @Override
    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        String json=rs.getString(columnIndex);
        return  JsonUtil.toMap(json);
    }

    @Override
    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json=cs.getString(columnIndex);
        return  JsonUtil.toMap(json);
    }
}
